SUBROUTINE set_policy_vars
  USE commonvars
  IMPLICIT NONE

  REAL(8)            :: pr_audit
  INTEGER            :: i
  
  ! Variables used in the estimation of the model
  Nnterms = 2
  n_times_wage = 1d0
  flag_no_run = 0
  paudit(0:1,1:Npaudits) = RESHAPE((/ 0.95d0, 0.05d0, 0.832d0, 0.168d0 /), SHAPE = (/2, Npaudits/)) ! (/ prob no audit, prob audit /)
  prob_conv = 0.76d0
  flag_audit_nterm = 0  
  
  ! Choose the policy
  IF (flag_base_policy == 1) THEN
     paudit(0:1,1:Npaudits) = RESHAPE((/ 0.95d0, 0.05d0, 0.95d0, 0.05d0 /), SHAPE = (/2, Npaudits/))
  ELSE IF (flag_prau_policy == 1) THEN
     pr_audit = 0.168d0
     paudit(0:1,1:Npaudits) = RESHAPE((/ 1d0-pr_audit, pr_audit, 1d0-pr_audit, pr_audit /), SHAPE = (/2, Npaudits/))
!!$     paudit(0:1,1:Npaudits) = RESHAPE((/ 0.832d0, 0.168d0, 0.832d0, 0.168d0 /), SHAPE = (/2, Npaudits/))
     pc_cost_prau = 1.48d0
  ELSE IF (flag_norun_policy == 1) THEN
     paudit(0:1,1:Npaudits) = RESHAPE((/ 0.95d0, 0.05d0, 0.95d0, 0.05d0 /), SHAPE = (/2, Npaudits/))
     flag_no_run = 1
  ELSE IF (flag_norun_prob_policy == 1) THEN
     paudit(0:1,1:Npaudits) = RESHAPE((/ 0.95d0, 0.05d0, 0.95d0, 0.05d0 /), SHAPE = (/2, Npaudits/))
     prob_conv_norun = 1d0
     l_fr_stolen_x = 0d0
  ELSE IF (flag_terms_policy == 1) THEN
     paudit(0:1,1:Npaudits) = RESHAPE((/ 0.95d0, 0.05d0, 0.95d0, 0.05d0 /), SHAPE = (/2, Npaudits/))
     Nnterms = 3
  ELSE IF (flag_wage_policy == 1) THEN
     paudit(0:1,1:Npaudits) = RESHAPE((/ 0.95d0, 0.05d0, 0.95d0, 0.05d0 /), SHAPE = (/2, Npaudits/))
     n_times_wage = 2d0
     pc_cost_wage(1) = 0.7059904d0
     pc_cost_wage(2) = 0.2450834d0
     pc_cost_wage(3) = 0.0580153d0
  ELSE IF (flag_hfine_policy == 1) THEN
     paudit(0:1,1:Npaudits) = RESHAPE((/ 0.95d0, 0.05d0, 0.95d0, 0.05d0 /), SHAPE = (/2, Npaudits/))
     per_ch_fine = per_ch_fine_out
  ELSE IF (flag_probconv_policy == 1) THEN
     paudit(0:1,1:Npaudits) = RESHAPE((/ 0.95d0, 0.05d0, 0.95d0, 0.05d0 /), SHAPE = (/2, Npaudits/))
     prob_conv = prob_conv_out
  ELSE IF (flag_terms_norun_policy == 1) THEN
     paudit(0:1,1:Npaudits) = RESHAPE((/ 0.95d0, 0.05d0, 0.95d0, 0.05d0 /), SHAPE = (/2, Npaudits/))
     Nnterms = 3
     flag_no_run = 1
  ELSE IF (flag_terms_3th_policy == 1) THEN
     paudit(0:1,1:Npaudits) = RESHAPE((/ 0.95d0, 0.05d0, 0.95d0, 0.05d0 /), SHAPE = (/2, Npaudits/))
     Nnterms = 3
     flag_audit_nterm = 1
     paudit_nterm(0:1) = (/ 0.832d0, 0.168d0 /)
  ELSE IF (flag_terms_norun_3th_policy == 1) THEN
     paudit(0:1,1:Npaudits) = RESHAPE((/ 0.95d0, 0.05d0, 0.95d0, 0.05d0 /), SHAPE = (/2, Npaudits/))
     Nnterms = 3
     flag_no_run = 1
     flag_audit_nterm = 1
     paudit_nterm(0:1) = (/ 0.832d0, 0.168d0 /)
  ELSE IF (flag_norun_2th_policy == 1) THEN
     paudit(0:1,1:Npaudits) = RESHAPE((/ 0.95d0, 0.05d0, 0.95d0, 0.05d0 /), SHAPE = (/2, Npaudits/))
     flag_no_run = 1
     flag_audit_nterm = 1
     paudit_nterm(0:1) = (/ 0.832d0, 0.168d0 /)
  ELSE IF (flag_terms_norun_wage_policy == 1) THEN
     paudit(0:1,1:Npaudits) = RESHAPE((/ 0.905d0, 0.095d0, 0.905d0, 0.095d0 /), SHAPE = (/2, Npaudits/))
     Nnterms = 3
     flag_no_run = 1
     n_times_wage = 2d0
  ELSE IF (flag_prau_wage_policy == 1) THEN
     pr_audit = 0d0 !0.168d0
     paudit(0:1,1:Npaudits) = RESHAPE((/ 1d0-pr_audit, pr_audit, 1d0-pr_audit, pr_audit /), SHAPE = (/2, Npaudits/))
     n_times_wage = 1d0
  ELSE IF (flag_terms_norun_prauk_policy == 1) THEN
     paudit(0:1,1:Npaudits) = RESHAPE((/ 0.905d0, 0.095d0, 0.905d0, 0.095d0 /), SHAPE = (/2, Npaudits/))
     Nnterms = 3
     flag_no_run = 1
  ELSE IF (flag_1term_policy == 1) THEN
     paudit(0:1,1:Npaudits) = RESHAPE((/ 0.95d0, 0.05d0, 0.95d0, 0.05d0 /), SHAPE = (/2, Npaudits/))
     Nnterms = 1
  ELSE IF (flag_optimal_prau_policy == 1) THEN
     pr_audit = pr_audit_out
     paudit(0:1,1:Npaudits) = RESHAPE((/ 1d0-pr_audit, pr_audit, 1d0-pr_audit, pr_audit /), SHAPE = (/2, Npaudits/))
     pc_cost_prau = pc_cost_prau_out
  ELSE IF (flag_term_optimal_prau_policy == 1) THEN
     pr_audit = pr_audit_out
     paudit(0:1,1:Npaudits) = RESHAPE((/ 1d0-pr_audit, pr_audit, 1d0-pr_audit, pr_audit /), SHAPE = (/2, Npaudits/))
     pc_cost_prau = pc_cost_prau_out
     Nnterms = 3
  ELSE IF (flag_norun_optimal_prau_policy == 1) THEN
     pr_audit = pr_audit_out
     paudit(0:1,1:Npaudits) = RESHAPE((/ 1d0-pr_audit, pr_audit, 1d0-pr_audit, pr_audit /), SHAPE = (/2, Npaudits/))
     pc_cost_prau = pc_cost_prau_out
     flag_no_run = 1
  ELSE IF (flag_term_norun_optimal_prau_policy == 1) THEN
     pr_audit = pr_audit_out
     paudit(0:1,1:Npaudits) = RESHAPE((/ 1d0-pr_audit, pr_audit, 1d0-pr_audit, pr_audit /), SHAPE = (/2, Npaudits/))
     pc_cost_prau = pc_cost_prau_out
     Nnterms = 3
     flag_no_run = 1
  ELSE IF (flag_term_norun_optimal_prau_lastterm_policy == 1) THEN
     paudit(0:1,1:Npaudits) = RESHAPE((/ 0.95d0, 0.05d0, 0.95d0, 0.05d0 /), SHAPE = (/2, Npaudits/))
     flag_audit_nterm = 1
     pr_audit = pr_audit_out
     paudit_nterm(0:1) = (/ 1d0-pr_audit, pr_audit /)
     pc_cost_prau = pc_cost_prau_out
     Nnterms = 3
     flag_no_run = 1     
  ELSE IF (flag_wage_optimal_prau_policy == 1) THEN
     pr_audit = pr_audit_out
     paudit(0:1,1:Npaudits) = RESHAPE((/ 1d0-pr_audit, pr_audit, 1d0-pr_audit, pr_audit /), SHAPE = (/2, Npaudits/))
     pc_cost_prau = pc_cost_prau_out
     n_times_wage = 2d0
     pc_cost_wage(1) = 0.7059904d0
     pc_cost_wage(2) = 0.2450834d0
     pc_cost_wage(3) = 0.0580153d0
  ELSE IF (flag_optimal_wage_policy == 1) THEN
     paudit(0:1,1:Npaudits) = RESHAPE((/ 0.95d0, 0.05d0, 0.95d0, 0.05d0 /), SHAPE = (/2, Npaudits/))
     pc_cost_prau = 0d0
     n_times_wage = n_times_wage_out
     DO i = 1, 3
        pc_cost_wage(i) = pc_cost_wage_out(i)
     END DO
  END IF

  wage_mayor(:) = n_times_wage*(/ 3233.379d0, 4267.472d0, 5076.549d0 /)/norm

  IF (myrank == 0) THEN
     write(*,*) 'flag_estimation', flag_estimation
     write(*,*) 'flag_base_policy', flag_base_policy
     write(*,*) 'flag_prau_policy', flag_prau_policy
     write(*,*) 'flag_norun_policy', flag_norun_policy
     write(*,*) 'flag_terms_policy', flag_terms_policy
     write(*,*) 'flag_wage_policy', flag_wage_policy
     write(*,*) 'flag_terms_norun_policy', flag_terms_norun_policy
     write(*,*) 'flag_terms_3th_policy', flag_terms_3th_policy
     write(*,*) 'flag_terms_norun_3th_policy', flag_terms_norun_3th_policy
     write(*,*) 'flag_norun_2th_policy', flag_norun_2th_policy
     write(*,*) 'flag_terms_norun_wage_policy', flag_terms_norun_wage_policy
     write(*,*) 'flag_terms_norun_prauk_policy', flag_terms_norun_prauk_policy
     write(*,*) 'flag_1term_policy', flag_1term_policy
     write(*,*) 'flag_no_selec_mun', flag_no_selec_mun
     write(*,*) 'flag_no_selec_funds', flag_no_selec_funds
     write(*,*) 'flag_no_selec_zzpr', flag_no_selec_zzpr
     write(*,*) 'flag_no_selec_age', flag_no_selec_age
     write(*,*) 'flag_no_selec_educ', flag_no_selec_educ
     write(*,*) 'flag_no_selec_sav', flag_no_selec_sav
     write(*,*) 'flag_no_selec_ab', flag_no_selec_ab
     write(*,*) 'flag_optimal_prau_policy', flag_optimal_prau_policy
     write(*,*) 'flag_term_optimal_prau_policy', flag_term_optimal_prau_policy
     write(*,*) 'flag_norun_optimal_prau_policy', flag_norun_optimal_prau_policy
     write(*,*) 'flag_term_norun_optimal_prau_policy', flag_term_norun_optimal_prau_policy
     write(*,*) 'flag_optimal_wage_policy', flag_optimal_wage_policy     
     write(*,*) 'flag_hfine_policy', flag_hfine_policy
     write(*,*) 'flag_no_change_ability', flag_no_change_ability
     write(*,*) 'flag_norun_prob_policy', flag_norun_prob_policy
  END IF

  !Policy variables
  IF (myrank == 0) write(*,*) 'fine_coeff', fine_coeff
  IF (myrank == 0) write(*,*) 'Nnterms', Nnterms
  IF (myrank == 0) write(*,*) 'n_times_wage', n_times_wage
  IF (myrank == 0) write(*,*) 'flag_no_run', flag_no_run
  IF (myrank == 0) write(*,*) 'flag_no_run_x', flag_no_run_x
  IF (myrank == 0) write(*,*) 'flag_policy_audit', flag_policy_audit 
  IF (myrank == 0) write(*,*) 'flag_audit_nterm', flag_audit_nterm
  IF (myrank == 0) write(*,*) 'pr_audit', pr_audit
  IF (myrank == 0) write(*,*) 'pc_cost_prau', pc_cost_prau
  IF (myrank == 0) write(*,*) 'n_times_wage', n_times_wage
  IF (myrank == 0) write(*,*) 'pc_cost_wage', pc_cost_wage
  IF (myrank == 0) write(*,*) 'w0', w0
  IF (myrank == 0) write(*,*) 'wealth', wealth
  IF (myrank == 0) write(*,*) 'subpcons', subpcons
  IF (myrank == 0) write(*,*) 'subqcons_vec', subqcons_vec
  IF (myrank == 0) write(*,*) 'ln_relqcons1', qconsvec(1,:)
  IF (myrank == 0) write(*,*) 'ln_relqcons2', qconsvec(2,:)
  IF (myrank == 0) write(*,*) 'ln_relqcons3', qconsvec(3,:)
  IF (myrank == 0) write(*,*) 'steal', stealing
  IF (myrank == 0) write(*,*) 'wage_mayor', wage_mayor
  IF (myrank == 0) write(*,*) 'norm', norm
  IF (myrank == 0) write(*,*) 'paudit(0,:)', paudit(0,:)
  IF (myrank == 0) write(*,*) 'paudit(1,:)', paudit(1,:)
  IF (myrank == 0) write(*,*) 'n_times_wage', n_times_wage
  IF (myrank == 0) write(*,*) 'fine_coeff', fine_coeff
  IF (myrank == 0) write(*,*) 'flag_trunc_norm', flag_trunc_norm
  IF (myrank == 0) write(*,*) 'Nsim', Nsim
  IF (myrank == 0) write(*,*) 'stealing', stealing
  IF (myrank == 0) write(*,*) 'fr_stoleni', fr_stoleni
  IF (myrank == 0) write(*,*) 'probedu', probedu
  IF (myrank == 0) write(*,*) 'per_ch_fine', per_ch_fine
  IF (myrank == 0) write(*,*) 'prob_conv', prob_conv
  IF (myrank == 0) write(*,*) 'prob_conv_norun', prob_conv_norun  
  IF (myrank == 0) write(*,*) 'l_fr_stolen_x', l_fr_stolen_x
  IF (myrank == 0) write(*,*) 'ch_mu_a', ch_mu_a
  
  !Careful with allocation of betapmayortemp1,2 using Nage, but depends on the actual grid used in emaxfuntemp.f90 !
  ALLOCATE(betanewcr(Tend,Nmun,Npast,Nnterms,Npaudits,Neduc,Nelap,MMability_steal_q), &
           betanornew(Tend,Nmun,Npast,Npaudits,Neduc,Nelap,Nwealth), &
           betasimrun(Tend,Nmun,Npast,Nnterms,Npaudits,Neduc,Nelap,MMability_steal_q), &
           betapmayor(Tend,Nmun,Nnterms,Npast,Neduc,Npast_t,Nwealth,Npaudits,Neduc,Nelap,MMage_steal_q), &
           betapmayortemp1(Tend,Nmun,Nnterms,Tend,Npast,Neduc,Npast_t,Nwealth,Npaudits,Neduc,Nelap,MMsteal_q), &
           betapmayornor(Tend,Nmun,Npast_t,Npaudits,Neduc,Nelap,Nwealth), &
           beta_voter_i(Tend,Nmun,Nnterms,Npast,Neduc,Npaudits,Neduc,Nelap,MMage_steal_q), &
           beta_voter_c(Tend,Nmun,Neduc,Npaudits,Neduc,Nelap,MMage))

  betanewcr = 0d0
  betanornew = 0d0
  betasimrun =0d0 
  betapmayor = 0d0
  betapmayortemp1 = 0d0
  betapmayornor = 0d0

END SUBROUTINE set_policy_vars
